package com.bawei.module_home.ui.merchant.profiles

import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_home.R
import com.bawei.module_home.adapter.ProfilesAdapter
import com.bawei.module_home.databinding.ActivityProfilesBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.ProfilesModelItem
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.PROFILES_PAGE)
class ProFilesActivity : BaseActivity<HomeServiceViewModel, ActivityProfilesBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_profiles
    val viewModel: HomeServiceViewModel by viewModels()
    val adapter: ProfilesAdapter by lazy {
        ProfilesAdapter()
    }
    override fun initView() {
        super.initView()
        initAdapter()
        mBinding.toobar.apply {
            title.text="企业档案"
            more.setImageResource(R.drawable.add_grey)
            more.setOnClickListener({
                ARouter.getInstance().build(Router.MERCHANT_ADD_PAGE).navigation()
            })
            back.setOnClickListener({
                finish()
            })
        }
        adapter.setOnItemClickListener(object : ProfilesAdapter.OnItemClickListener{
            override fun clickListener(
                item: ProfilesModelItem,
                position: Int
            ) {
                ARouter.getInstance().build(Router.PROFILES_DETAIL_PAGE).withInt("profileId",viewModel.proFilesList[position].id).navigation()
            }

        })

        mBinding.search.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String): Boolean {
                searchAreaSpaces(query)
                return true
            }

            override fun onQueryTextChange(newText: String): Boolean {
                searchAreaSpaces(newText)
                return true
            }

        })
    }

    fun searchAreaSpaces(query: String){
        if(TextUtils.isEmpty(query)){
            adapter.setData(viewModel.proFilesList)
        }else{
            viewModel.newProFilesList.clear()
            var lowerCaseQuery=query.toLowerCase()
            for (modelItem in viewModel.proFilesList) {
                if(modelItem.name.contains(lowerCaseQuery)){
                    viewModel.newProFilesList.add(modelItem)
                }
            }
            adapter.setData(viewModel.newProFilesList)
        }

    }

    override fun initData() {
        viewModel.getProfiles()
    }

    override fun initListener() {
        viewModel.mProfilesState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    adapter.setData(viewModel.proFilesList)
                }
            }
        }
    }

    fun initAdapter(){
        mBinding.rv.layoutManager= LinearLayoutManager(this)
        mBinding.rv.adapter=adapter
    }

    override fun onResume() {
        super.onResume()
        viewModel.getProfiles()
    }

}